package flc.ast.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.util.BannerUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyCollectBean;
import flc.ast.utils.AppSpUtils;
import stark.common.bean.StkResBean;

public class MyBannerAdapter extends BannerAdapter<StkResBean, MyBannerAdapter.ImageTitleHolder> {

    Context context;

    public MyBannerAdapter(List<StkResBean> datas, Context context) {
        super(datas);
        this.context = context;
    }

    private int selPosition = 0;

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    public ImageTitleHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new ImageTitleHolder(BannerUtils.getView(parent, R.layout.item_banner));
    }

    @Override
    public void onBindView(ImageTitleHolder holder, StkResBean data, int position, int size) {
        Glide.with(holder.itemView).load(data.getThumbUrl()).into(holder.ivBannerItemImg);
        holder.tvBannerItemName.setText(data.getName());
        if (data.isSelected()) {
            holder.ivBannerItemCollect.setImageResource(R.drawable.ysc1);
        } else {
            holder.ivBannerItemCollect.setImageResource(R.drawable.wsc1);
        }
        try {
            Gson gson = new Gson();
            String json = gson.toJson(data.getExtra());
            JSONObject jsonObject = new JSONObject(json);
            String releaseDate = jsonObject.getString("releaseDate");
            int index = releaseDate.indexOf("(");
            String dateStr = releaseDate.substring(0, index);
            Date date = TimeUtils.string2Date(dateStr, new SimpleDateFormat("yyyy-MM-dd"));
            holder.tvBannerItemDate.setText(context.getString(R.string.release_date) + TimeUtils.date2String(date, new SimpleDateFormat("yyyy年MM月dd日")));
            holder.ivBannerItemCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<MyCollectBean> listSp = AppSpUtils.getCollectList();
                    if (data.isSelected()) {
                        for (MyCollectBean myCollectBean : listSp) {
                            if (myCollectBean.getPath().equals(data.getThumbUrl())) {
                                listSp.remove(myCollectBean);
                                break;
                            }
                        }
                        AppSpUtils.saveCollectList(listSp);
                        data.setSelected(false);
                    } else {
                        List<MyCollectBean> listSave = new ArrayList<>();
                        listSave.add(new MyCollectBean(data.getName(), data.getThumbUrl(), date.getTime()));
                        if (listSp != null && listSp.size() != 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveCollectList(listSp);
                        } else {
                            AppSpUtils.saveCollectList(listSave);
                        }
                        data.setSelected(true);
                    }
                    if (data.isSelected()) {
                        holder.ivBannerItemCollect.setImageResource(R.drawable.ysc1);
                    } else {
                        holder.ivBannerItemCollect.setImageResource(R.drawable.wsc1);
                    }
                }
            });
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public class ImageTitleHolder extends RecyclerView.ViewHolder {
        public ImageView ivBannerItemImg, ivBannerItemCollect;
        public TextView tvBannerItemName, tvBannerItemDate;

        public ImageTitleHolder(@NonNull View view) {
            super(view);
            ivBannerItemImg = view.findViewById(R.id.ivBannerItemImg);
            ivBannerItemCollect = view.findViewById(R.id.ivBannerItemCollect);
            tvBannerItemName = view.findViewById(R.id.tvBannerItemName);
            tvBannerItemDate = view.findViewById(R.id.tvBannerItemDate);

        }
    }
}
